﻿using CommunityToolkit.Mvvm.ComponentModel;
using GameView.Comment;
using GameView.Models;
using GameView.Services.Dtos.Project;
using System.Net.Http;
using System.Net.Http.Json;

namespace GameView.Services.Comment
{
    public partial class ProjectService : ObservableObject
    {
        private readonly HttpClient _httpClient;

        [ObservableProperty]
        private bool _isLoading;
        public ProjectService()
        {
            _httpClient = new HttpClient
            {
                BaseAddress = new Uri(APIHelper.Project)
            };
        }
        public async Task<ApiResponse<List<ProjectWithGamesInfo>>> GetByPersonIdHAsync()
        {
            IsLoading = true;
            int pid = 0;
            if (StaticValues.LoginInfo.PersonInfo.ParentId > 0)
                pid = StaticValues.LoginInfo.PersonInfo.ParentId;
            else
                pid = StaticValues.LoginInfo.PersonInfo.Id;
            var res = await $"{APIHelper.Project}by-person-idh/{pid}".GetAsync<List<ProjectWithGamesInfo>, object>();
            IsLoading = false;
            return res;
        }
        public async Task<ApiResponse<List<ProjectWithGamesInfo>>> GetByPersonIdAsync()
        {
            IsLoading = true;
            int pid = 0;
            if (StaticValues.LoginInfo.PersonInfo.ParentId > 0)
                pid = StaticValues.LoginInfo.PersonInfo.ParentId;
            else
                pid = StaticValues.LoginInfo.PersonInfo.Id;
            var res = await $"{APIHelper.Project}by-person-id/{pid}".GetAsync<List<ProjectWithGamesInfo>, object>();
            IsLoading = false;
            return res;
        }
        public async Task<ApiResponse<List<ProjectWithGamesInfo>>> GetAllAsync()
        {
            IsLoading = true;
            var res = await $"{APIHelper.Project}all".GetAsync<List<ProjectWithGamesInfo>, object>();
            IsLoading = false;
            return res;
        }
        public async Task<ApiResponse<List<ProjectWithGamesInfo>>> GetAllHAsync()
        {
            IsLoading = true;
            var res = await $"{APIHelper.Project}allh".GetAsync<List<ProjectWithGamesInfo>, object>();
            IsLoading = false;
            return res;
        }
        public async Task<ApiResponse<bool>> AddNewPAsync(string name)
        {
            try
            {
                IsLoading = true;
                if (!_httpClient.SetToken())
                    return new() { Errors = "您的密钥缺失！" };
                var resp = await _httpClient.PostAsJsonAsync("", new { Name = name });
                var apiResponse = await resp.Content.ReadFromJsonAsync<ApiResponse<bool>>();
                if (apiResponse?.StatusCode == 200)
                {
                    return apiResponse;
                }
                return apiResponse ?? new ApiResponse<bool> { Errors = "Invalid API response" };
            }
            catch (Exception ex)
            {
                return new() { Errors = ex.Message };
            }
            finally
            {
                IsLoading = false;
            }
        }
        public async Task<ApiResponse<bool>> AddNewGAsync(int pid, string name)
        {
            try
            {
                IsLoading = true;
                var _httpClientG = new HttpClient
                {
                    BaseAddress = new Uri(APIHelper.Game)
                };
                if (!_httpClientG.SetToken())
                    return new() { Errors = "您的密钥缺失！" };
                var resp = await _httpClientG.PostAsJsonAsync("", new { PId = pid, Name = name });
                var apiResponse = await resp.Content.ReadFromJsonAsync<ApiResponse<bool>>();
                if (apiResponse?.StatusCode == 200)
                {
                    return apiResponse;
                }
                return apiResponse ?? new ApiResponse<bool> { Errors = "返回结果错误！" };
            }
            catch (Exception ex)
            {
                return new() { Errors = ex.Message };
            }
            finally
            {
                IsLoading = false;
            }
        }
        public async Task<ApiResponse<int>> DelPAsync(int id)
        {
            IsLoading = true;
            var res = await $"{APIHelper.Project}batch-delete".DeleteData<List<int>>([id]);
            IsLoading = false;
            return res;
        }
        public async Task<ApiResponse<int>> DelGAsync(int id)
        {
            IsLoading = true;
            var res = await $"{APIHelper.Game}/batch-delete".DeleteData<List<int>>([id]);
            IsLoading = false;
            return res;
        }
    }
}